﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Revis.VClinicNet.UI.External.Client.Service;
using MvvmFoundation.Wpf;
using System.Windows.Input;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Data;
using System.Security;
using Revis.VClinicNet.Util.Extensions;
using System.ServiceModel;

namespace Revis.VClinicNet.UI.External.Client.ViewModel
{
    public class LoginPasswordConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return new PasswordBoxPlainPasswordProvider(value as PasswordBox);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public interface IPlainPasswordProvider
    {
        string Password { get; }
    }

    public class PasswordBoxPlainPasswordProvider : IPlainPasswordProvider
    {
        private PasswordBox _passwordBox;

        public PasswordBoxPlainPasswordProvider(PasswordBox passwordBox)
        {
            if (passwordBox == null) {
                throw new ArgumentNullException("passwordBox");
            }
            _passwordBox = passwordBox;
        }

        public string Password
        {
            get
            {
                SecureString secureString = _passwordBox.SecurePassword;
                secureString.MakeReadOnly();
                return secureString.ToUnsecureString();
            }
        }
    }

    public class LoginViewModel : WorkspaceControlViewModelBase<View.Login>
    {
        public LoginViewModel() { }

        public LoginViewModel(string displayName, IPatientService ips) :
            base(displayName, ips)
        {
        }

        public void Request(IPlainPasswordProvider provider)
        {
            //FakePatientService service = (FakePatientService)_patientService;
            ClientBase<IPatientService> service = (ClientBase<IPatientService>)_patientService;
            service.ClientCredentials.UserName.UserName = UserPesel;
            service.ClientCredentials.UserName.Password = provider.Password;
            ChangeState("Oczekiwanie na autoryzacje", RequestState.Requesting);
            _patientService.BeginLogin(RequestEnded, null);
        }

        public void RequestEnded(IAsyncResult result)
        {
            try {
                _patientService.EndLogin(result);
                ChangeState("Logowanie zakończone", RequestState.Success);
            } catch (Exception) {
                ChangeState("Logowanie zakończone niepowodzeniem", RequestState.Faulted);
            }
        }

        public override void Run()
        {

        }

        private RelayCommand<IPlainPasswordProvider> _loginCommand = null;
        public ICommand LoginCommand
        {
            get
            {
                _loginCommand = _loginCommand ?? new RelayCommand<IPlainPasswordProvider>(p => Request(p));
                return _loginCommand;
            }
        }

        private string _userPesel = string.Empty;
        public string UserPesel
        {
            get { return _userPesel; }
            set
            {
                _userPesel = value;
                RaisePropertyChanged("UserPesel");
            }
        }
    }
}
